package com.pro.controller;

import com.pro.entry.JsBeforeUser;
import com.pro.entry.JsUser;
import com.pro.service.LoginService;
import com.pro.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

/**
 * @Author 谭金诚
 * @Description TODO 1
 * @Date 2022/3/31 11:23
 * @Version 1.02
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/getLogin")
    @ResponseBody
    public JsonData getLogin(JsBeforeUser beforeUser){
        JsBeforeUser login = loginService.getLogin(beforeUser);
        return login != null ? JsonData.buildSuccess(login) : JsonData.buildError("服务端错误");
    }

    @PostMapping("getLoginByPhone")
    @ResponseBody
    public JsonData getLoginByPhone(String phone){
        JsBeforeUser loginByPhone = loginService.getLoginByPhone(phone);
        return loginByPhone != null ? JsonData.buildSuccess(loginByPhone) : JsonData.buildError("服务端错误");
    }

    @GetMapping("/getLoginCode")
    @ResponseBody
    public JsonData getLoginCode(){
        int code = (int)((Math.random()*9+1)*100000);
        return JsonData.buildSuccess(code);
    }

    @PostMapping("/getLoginByWX")
    @ResponseBody
    public JsonData getLoginByWX(JsBeforeUser beforeUser){
        JsBeforeUser loginByWX = loginService.getLoginByWX(beforeUser);
        return loginByWX != null ? JsonData.buildSuccess(loginByWX) : JsonData.buildError("服务端错误");
    }

    @PostMapping("/updateUserPWD")
    @ResponseBody
    public JsonData updateUserPWD(String phone, String password){
        boolean flag = loginService.updateUserPWD(phone, password);
        return flag ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

    @PostMapping("/addBeforeUser")
    @ResponseBody
    public JsonData addBeforeUser(JsBeforeUser beforeUser){
        boolean flag = loginService.addBeforeUser(beforeUser);
        return flag ? JsonData.buildSuccess(flag) : JsonData.buildError("服务端错误");
    }

}
